package pers.linkong.asr.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import pers.linkong.asr.domain.CourseBean;
import pers.linkong.asr.domain.CourseSublevelBean;
import pers.linkong.asr.service.CourseService;
import pers.linkong.asr.util.CourseKit;
import pers.linkong.asr.util.LayPage;
import pers.linkong.asr.util.ResultInfo;

@Controller
@RequestMapping("/course")
public class CourseController {
	
	@Autowired
	private CourseService courseService;
	
	@RequestMapping("/add")
	@ResponseBody
	public ResultInfo addCourse(CourseBean course) throws IOException{
		try{
			System.out.println("试图添加" + course.getCid());
			this.courseService.addCourse(course);
			return ResultInfo.data(1, "保存成功");
		}
		catch(Exception e){
			e.printStackTrace();
			return ResultInfo.data(0, "发生异常，保存失败，请稍后再试");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ResultInfo updateCourse(CourseBean course) throws IOException{
		try{
			System.out.println("试图修改" + course.getCid());
			this.courseService.updateCourse(course);
			return ResultInfo.data(1, "保存成功");
		}
		catch(Exception e){
			e.printStackTrace();
			return ResultInfo.data(0, "发生异常，保存失败，请稍后再试");
		}
	}
	
	// 分页查询
	@RequestMapping("/findCourseByPage")
	@ResponseBody
	public LayPage findCourseByPage(String cid, String cname, String ssex,
			String sclass, String grade, String major, String tname,
			long page, long limit,  HttpSession session) throws Exception{
		
		CourseBean course = new CourseBean();
		long start;
		
		start = (page - 1) * limit;	

		// trim方法可以删除字符串前端和后端的空格（含tab键产生的空格）
		if(cid != null && cid != ""){
			course.setCid(Integer.parseInt(cid.trim()));
		}
		if(cname != null && cname != ""){
			course.setCname(cname.trim());
		}
		if(sclass != null && sclass != ""){
			course.setSclass(sclass.trim());
		} else {
			course.setSclass((String) session.getAttribute("sessionClass"));
		}
		if(major != null && major != ""){
			course.setMajor(major.trim());
		}
		if(tname != null && tname != ""){
			course.setTname(tname.trim());
		}
		List<CourseBean> courses
			= this.courseService.findCourseByPage(course, start, limit);
		long count = this.courseService.getCourseCount(course);
		
		System.out.println("CC:"+count);
		return LayPage.data(0, "", count, courses);
	}
	
	// 根据id查询
	@RequestMapping("/getCourseById")
	@ResponseBody
	public CourseBean getCourseById(int cid) throws Exception{
		System.out.println(cid);
		return this.courseService.getCourseById(cid);
	}
	
	// 根据id删除
	@RequestMapping("/deleteCourseById")
	@ResponseBody
	public ResultInfo deleteCourseById(int cid) throws IOException{
		try{
			this.courseService.deleteCourse(cid);
			return ResultInfo.data(1, "删除成功");
		}
		catch(Exception e){
			e.printStackTrace();
			return ResultInfo.data(0, "删除失败，请先删除该课程的所有课程安排");
		}
	}
	

	// 根据班级查询当前时间有无课程
	@RequestMapping("/getCourseBySclass")
	@ResponseBody
	public ResultInfo getCourseBySclass(String classname, HttpSession session) throws IOException{
		try{
			// 查询当前班级拥有的课程数量
			System.out.println(classname);
			if(classname == null || classname == "" || classname == "null"){
				return ResultInfo.data(1, "未输入任何班级");
			}
			String classid = this.courseService.getClassidByName(classname);
			if(classid == null || classid == ""){
				return ResultInfo.data(2, "未找到输入的班级");
			}
			long count = this.courseService.getCourseCountBySclass(classid);
			CourseSublevelBean csbean = null;
			if(count == 0){
				return ResultInfo.data(3, "该班级存在，但没有任何课程");
			} else {
				List<CourseSublevelBean> courseSublevelBeans = this.courseService.getCourseBySclass(classid);
				csbean = CourseKit.getCourseBySclass(courseSublevelBeans, courseService);
				if(csbean == null){
					return ResultInfo.data(4, "该班级存在，且拥有课程，但在当前时间段内没有合适的课程课程安排供选择");
				} else {
					csbean.setNowweeknum(this.courseService.getNowWeekNum(csbean.getCid()));
					ResultInfo resultInfo = ResultInfo.data(5, "该班级存在，拥有课程，且拥有符合所有时间条件的课程，已将该班级存至session", csbean);
					session.setAttribute("className",classname);
					session.setAttribute("classId",classid);
					return resultInfo;
				}
			}
		}
		catch(Exception e){
			e.printStackTrace();
			return ResultInfo.data(0, "发生异常，查询失败，请稍后再试");
		}
	}
	
	@RequestMapping("/setClass")
	@ResponseBody
	public ResultInfo setClass(String classname, HttpSession session) throws IOException {
		try{
			System.out.println(classname);
			if(classname == null || classname == "" || classname == "null"){
				return ResultInfo.data(1, "未输入任何班级");
			}
			String classid = this.courseService.getClassidByName(classname);
			if(classid == null || classid == ""){
				return ResultInfo.data(2, "未找到输入的班级");
			}
			session.setAttribute("className",classname);
			session.setAttribute("classId",classid);
			return ResultInfo.data(3, "已成功设置");
		}
		catch(Exception e){
			e.printStackTrace();
			return ResultInfo.data(0, "发生异常，设置失败，请稍后再试");
		}
	}
	
	// 获取当前登录教师的所有课程
	@RequestMapping("/getCourseByTid")
	@ResponseBody
	public LayPage getCourseByTid(HttpSession session) throws Exception{
		Object tid = session.getAttribute("loginTid");
		List<CourseBean> courses = this.courseService.getCourseByTid((int) tid);
		System.out.println("CC:"+courses.size());
		return LayPage.data(0, "", (long) courses.size(), courses);
	}
}